/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Frame_1_LoginFrame.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.ui;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import pkg_04_other.examApp.dao.ExaminationFactory;
import pkg_04_other.examApp.entity.Student;

/**
 * 登陆面板
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Frame_1_LoginFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = 7746451175798991826L;

	/** 按钮和标签组件 */
	private final JTextField jtf_id = new JTextField(15);
	private final JPasswordField jpf_pwd = new JPasswordField(15);
	private final JButton bt_login = new JButton("登陆");
	private final JButton bt_cancle = new JButton("取消");

	public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
	public static final int HEIGHT = Toolkit.getDefaultToolkit()
			.getScreenSize().height;

	/**
	 * 初始化面板
	 */
	private void init() {
		JPanel main_panel = new JPanel(new GridLayout(4, 1));

		JPanel jp1 = new JPanel();
		JLabel label1 = new JLabel("考生登陆");
		label1.setFont(new Font("黑体", Font.PLAIN, 20));
		jp1.add(label1);
		main_panel.add(jp1);

		JPanel jp2 = new JPanel();
		JLabel label2 = new JLabel("学号:");
		jp2.add(label2);
		jp2.add(jtf_id);
		main_panel.add(jp2);

		JPanel jp3 = new JPanel();
		JLabel label3 = new JLabel("密码:");
		jp3.add(label3);
		jp3.add(jpf_pwd);
		main_panel.add(jp3);

		JPanel jp4 = new JPanel();
		jp4.add(bt_login);
		jp4.add(bt_cancle);
		main_panel.add(jp4);

		this.add(main_panel, "Center");
	}

	public Frame_1_LoginFrame() {
		super("考生登陆");
		init();
		this.setSize(300, 200);
		this.setLocation((WIDTH - 300) / 2, (HEIGHT - 300) / 2);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 添加监听器
		bt_login.addActionListener(this);
		bt_cancle.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		try {
			if ("登陆".equals(command)) {
				int id = Integer.parseInt(jtf_id.getText());
				String password = new String(jpf_pwd.getPassword());
				Student student = new Student(id, "", password);
				if (ExaminationFactory.getExaminationInstance("student").login(
						student)) {
					new Frame_2_SelectSubjectFrame();
					// 弹出科目选择面板后, 登陆面板应该不可见
					setVisible(false);
				} else {
					JOptionPane.showMessageDialog(this, "登陆失败, 请重新登陆");
				}
			}
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(this, "登陆失败, 请重新登陆");
		}
		if ("取消".equals(command)) {
			jtf_id.setText("");
			jpf_pwd.setText("");
		}
	}
}
